package org.rcgwt.client.ui.grid.internal;

import org.rcgwt.client.ui.grid.internal.model.IColumnModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IElementModelDelta;
import org.rcgwt.client.ui.grid.internal.model.IGridModel;
import org.rcgwt.client.ui.grid.model.IColumn;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class ColumnModel extends AbstractElementModel implements IColumnModel {

    private final IGridModel gridModel;

    private final IColumn column;

    private OrderingMark orderingMark = OrderingMark.NONE;

    public ColumnModel(IGridModel gridModel, IColumn column, int columnIndex) {
        set(columnIndex, 0);

        this.gridModel = gridModel;

        this.column = column;
    }

    @Override
    public Type getType() {
        return Type.COLUMN;
    }

    @Override
    public String toString() {
        return "[Column modelIndex=" + modelIndex + " state=0x"
                + Integer.toHexString(stateMask) + "]";
    }

    @Override
    public IElementModel getParent() {
        return gridModel;
    }

    @Override
    public IGridModel getGridModel() {
        return gridModel;
    }

    @Override
    public IColumn getColumn() {
        return column;
    }

    @Override
    public OrderingMark getOrderingMask() {
        return orderingMark;
    }

    @Override
    public void setOrderingMask(OrderingMark newOrderingMark) {
        if (newOrderingMark == this.orderingMark) {
            return;
        }

        OrderingMark oldOrderingMask = this.orderingMark;

        this.orderingMark = newOrderingMark;

        ((ColumnModelDelta) getModelElementDelta()).performOrderingMaskChanged(
                oldOrderingMask, newOrderingMark);
    }

    @Override
    public IElementModelDelta< ? > createDelta() {
        return new ColumnModelDelta(this);
    }

}